package com.creative.accountsdk.adapter;

import android.content.Context;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.creative.accountsdk.R;
import com.creative.accountsdk.bean.ClassInfo;

import java.util.List;
/**
 * 教师加入班级
 *    班级列表
 *
 * @author  David
 * @version 1.0
 * @created 2015-11-25
 */
public class ClassAdapter extends BaseAdapter implements View.OnClickListener {

    private Context mContext;
    private List<ClassInfo> list;
    private Callback mCallback;

    /*** 自定义接口，用于回调按钮点击事件到Activity*/
    public interface Callback {
        public void click(View v);
    }
    public ClassAdapter(Context context,Callback callback){
        this.mContext = context;
        this.mCallback = callback;
    }

    public void setData( List<ClassInfo> list){
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int postion, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (view == null) {
            viewHolder = new ViewHolder();
            view = LayoutInflater.from(mContext).inflate( R.layout.teacher_class_adapter, null);
           viewHolder.rlToCourse = (RelativeLayout) view.findViewById(R.id.rl_to_course);
            viewHolder.tvName = (TextView) view.findViewById(R.id.tv_class_name);
            viewHolder.btnAdd = (Button) view.findViewById(R.id.btn_add_class);
            viewHolder.ivAdd = (ImageView)view.findViewById(R.id.iv_add_class);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        viewHolder.btnAdd.setOnClickListener(this);
        viewHolder.btnAdd.setTag(postion);

        viewHolder.ivAdd.setOnClickListener(this);
        viewHolder.ivAdd.setTag(postion);

        ClassInfo info = list.get(postion);
        if(info.type==1){
            viewHolder.ivAdd.setVisibility(View.VISIBLE);
            viewHolder.btnAdd.setVisibility(View.GONE);
        }else{
            viewHolder.ivAdd.setVisibility(View.GONE);
            viewHolder.btnAdd.setVisibility(View.VISIBLE);
        }

        viewHolder.tvName.setText(info.name);

        return view;
    }

    @Override
    public void onClick(View v) {
        mCallback.click(v);
    }

    class ViewHolder {
        RelativeLayout  rlToCourse;
        TextView   tvName;
        Button     btnAdd;
        ImageView  ivAdd;
    }
}
